{template head}
<script type="text/javascript">
var ajaxparam = {
		'url' : '{SITE_URL}/ajax/index.php'
};

function searchCP(p)
{
	$('#page').val(p);
	getCPList();
}

function getCPList()
{
	var data = {};
	data.page = $('#page').val();
	data.ptid = $('#pt_id').val();
	data.m = 'getCPList';
	var url = ajaxparam.url;
	var callback = function(data)
	{
		if(data.sign == -2)
		{
			window.location = data.url;
		}
		else
		{
			if(data.sign == 1)
			{
				var getTpl = $('#chanpin_temp').html();
				laytpl(getTpl).render(data, function(html){
				  	$('#chanpin_list').html(html);
				});
				
				var pageTpl = $('#chanpin_page_temp').html();
				laytpl(pageTpl).render(data, function(html){
				  	$('#chanpin_page').html(html);
				});
			}
			else
			{
				if(data.msg)
				{
					layer.msg(data.msg);
				}
				else
				{
					layer.msg('操作失败');
				}
			}
		}
	};
	makeAjax('post',url,data,callback);
	
}

function addItem(id,str)
{
	if(id)
	{
		if($("#cplist option[value='"+id+"']").length == 0)
		{
			$("#cplist").append('<option label="'+str+'" value="'+id+'"></option>');
		}
		else
		{
			layer.msg('重复添加');
		}
	}
	else
	{
		layer.msg('缺少ID');
	}
}

 //上移  
function moveUp()
{
	if(null == $('#cplist').val())
	{
		layer.msg('请选择移动项');
		return false; 
	} 
		//选中的索引,从0开始 
	var optionIndex = $('#cplist').get(0).selectedIndex; 
		//如果选中的不在最上面,表示可以移动 
	if(optionIndex > 0)
	{
		$('#cplist option:selected').insertBefore($('#cplist option:selected').prev('option')); 
	}

}  
  
//下移  
function moveDown()
{
	if(null == $('#cplist').val())
	{
		layer.msg('请选择移动项');
		return false; 
	} 
	//索引的长度,从1开始 
	var optionLength = $('#cplist')[0].options.length; 
	//选中的索引,从0开始 
	var optionIndex = $('#cplist').get(0).selectedIndex; 
	//如果选择的不在最下面,表示可以向下 
	if(optionIndex < (optionLength-1))
	{
		$('#cplist option:selected').insertAfter($('#cplist option:selected').next('option')); 
	}  
}

function delItem()
{
	if(null == $('#cplist').val())
	{
		layer.msg('请选择删除项');
		return false; 
	} 
	$('#cplist option:selected').remove(); 
}

function saveItem()
{
	var data = {};
	
	var items = [];
	
	$('#cplist option').each(function(){
		items.push($(this).val());
	})
	
	data.items = items;
	
	var url = '{SITE_URL}ajax/index.php?m=saveIndexCP';
	
	var callback = function(data)
	{
		if(data.sign == -2)
		{
			window.location = data.url;
		}
		else
		{
			if(data.sign == 1)
			{
				//添加成功,返回列表页
				layer.alert(data.msg,function(){
					window.location = window.location;
				});
			}
			else
			{
				if(data.msg)
				{
					layer.msg(data.msg);
				}
				else
				{
					layer.msg('操作失败');
				}
			}
		}
	};
	makeAjax('post',url,data,callback);
}
</script>
<body>
<div class="wrapper">
	<div class="crumbs"><span style="margin: 0 5px;color: #333;font-size: 14px;">内容管理</span> &gt; <span>首页产品</span> </div>
	
	<div class="container_tit clearfix mb20 ft14">
		<input type="hidden" name="page" id="page" value="1" />
        <div class="choose fl mr15" style="padding-top: 5px;">
        	<span>平台列表</span>
            <select class="select w150" name="pt_id" id="pt_id">
            	<option value="">请选择</option>
            	{loop $ptlist $k $v}
            	<option value="{$v[id]}">{$v[pt_name]}</option>
            	{/loop}
            </select>
        </div>
        
        <div class="btn_box fl" style="padding-top: 5px;">
        	<input type="button" name="submit" class="btnblue mr15" value="搜索" onclick="searchCP('1');">
        </div>
    </div>
	<ul>
		<li style="width:60%;" class="fl">
			<!-- list -->
			<table class="table">
				<thead>
					<th width="">平台名称</th>
					<th width="">产品名称</th>
					<th width="">投资期限</th>
		        	<th width="">操作</th>
				</thead>
				<script id="chanpin_temp" type="text/html">
					{{# layui.each(d.list, function(index, item){ }}
						<tr>
							<td style="text-align: center;">{{item.pt_name}}</td>
							<td style="text-align: center;">{{item.cp_name}}</td>
							<td style="text-align: center;">{{item.qixian}}</td>
							<td style="text-align: center;">
								<a onclick="addItem('{{item.id}}','{{item.pt_name}}-{{item.cp_name}}');" class="btn_add" href="javascript:;"  title="添加产品">操作</a>
							</td>
						</tr>
					{{# }); }}
				</script>
				<script id="chanpin_page_temp" type="text/html">
					<a onclick="searchCP('1');" href="javascript:;">首页</a>
					<a onclick="searchCP('{{d.page._prepage}}');" href="javascript:;">上一页</a>
					{{# layui.each(d.page.pagelist, function(index, item){ }}
						{{# if(item.p === d.page._page){ }}	
							<a onclick="searchCP('{{item.p}}');" class="current" href="javascript:;">{{item.p}}</a>
						{{# } else { }}
							<a onclick="searchCP('{{item.p}}');" href="javascript:;">{{item.p}}</a>
						{{#  } }}
					{{# }); }}
					<a onclick="searchCP('{{d.page._nextpage}}');" href="javascript:;">下一页</a>
					<a onclick="searchCP('{{d.page._totlepage}}');" href="javascript:;">尾页</a>
				</script>
				<tbody id="chanpin_list">
				</tbody>
			</table>
			<div style="padding-top:10px;" id="chanpin_page" class="d_page"></div>
		</li>
		<li style="width:40%;" class=" fl">
			<div style="text-align: center;">
				<select style="width: 350px;" class="mr15" id="cplist" multiple size="10">
					{loop $list $k $v}
					<option value="{$v[id]}">{$v['pt_name']}-{$v['cp_name']}</option>
					{/loop}
				</select>
			</div>
			<div style="padding-top:20px;text-align: center;">
				<input type="button" class="btnblue mr15" value="上移" onclick="moveUp();">
				<input type="button" class="btnblue mr15" value="下移" onclick="moveDown();">
				<input type="button" class="btnyellow mr15" value="删除" onclick="delItem();">
				<input type="button" class="btnblue " value="保存" onclick="saveItem();">
					
			</div>
			
			
		</li>
	</ul>
	
</div>
{template foot}